package com.rqf.ls.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rqf.ls.service.ILoginOfLawyerService;
import com.server.password.MyMD5;

@Service("LoginOfLawyerService")
public class LoginOfLawyerService implements ILoginOfLawyerService {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	 /**
	 * 手机+密码登录验证
	 * @param name
	 * @param password
	 * @return 手机号
	 * @author ServerZhang
	 * @date 2016年3月28日
	 */
	@Override
	public Map<String, Object> login(String name, String password){
		Map<String, Object> returnMap = new HashMap<String, Object>();
		String phoneNo = null;
		String infor = null;
		Boolean isSuccess = false;
		Boolean checked = false;
		if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)) {
			infor = "账号或密码不能为空。";
			returnMap.put("phoneNo", phoneNo);
			returnMap.put("isSuccess", isSuccess);
			returnMap.put("infor", infor);
			return returnMap;
		}
		//MD5经过3次加密
		String encryptStr = MyMD5.circularlyEncrypt(password, 3);
		
		// 验证用户
		String sql = "Select distinct phone,INBLACKLIST,CHECKED from lsuser where Phone='%s' and userpwd='%s' and ABLE=1";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, name, encryptStr));
		if (list.size() > 0) {
			Map<String, Object> map = list.get(0);
			Object phoneObj = map.get("phone");
			if (phoneObj != null) {
				phoneNo = phoneObj.toString();
			}
			Object inBlackObj = map.get("INBLACKLIST");
			Object checkedObj = map.get("CHECKED");
			if (inBlackObj != null && "1".equals(inBlackObj.toString())) {
				infor = "您的账号出现异常，暂时停用。";
			}else if(checkedObj == null || "0".equals(checkedObj.toString())){
				infor = "登录成功，但您的账号暂未通过审核。";
				isSuccess = true;
			}else {
				infor = "登录成功。";
				isSuccess = true;
				checked = true;
			}
		}else {
			infor = "账号或密码错误！";
			isSuccess = false;
		}
		returnMap.put("phoneNo", phoneNo);
		returnMap.put("isSuccess", isSuccess);
		returnMap.put("infor", infor);
		returnMap.put("checked", checked);
		return returnMap;
	}
}
